<?
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
//    [ am... ]
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

define('_ref_resolved_chars', '\sa-zа-яё0-9\-\.\,\:\;\!\№\#\@\&\(\)');


function check_rfc_item($name)
{
   $name = str_replace(' ', '&nbsp;', $name);
   $name = preg_replace('/[^'._ref_resolved_chars.']/iu', '', $name);

   if(strlen($name)<3) return false;
   
   return $name;
}

function trim_spaces_rfc($name)
{
   $name = str_replace('&nbsp;', ' ', $name);
   
   $name = preg_replace('/(\s*)(.+)(\s*)/u', '$2', $name);

   $name = trim($name);

   return $name;
}

function nbsp($str)
{
   return str_replace('&nbsp;', ' ', $str);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function get_noitem()
{
   return array('id'=>0, 'name'=>'n/a', 'num'=>0);
}

function get_rfc_item($base, $id)
{
   if(!($id>0)) return get_noitem();
   
   $db = mysql_init();

   $q = 'select * from ?# where `id`=?d limit 1';

   $ref = $db->selectRow($q, $base, $id);

   if( isset($ref['id']) ) return $ref;
   else
   {
      return get_noitem();
   }
}

//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

function get_rfc_item_by_num($base, $num)
{
   if(!($num>0)) return get_noitem();

   $db = mysql_init();

   $q = 'select * from ?# where `num`=?d limit 1';

   $ref = $db->selectRow($q, $base, $num);

   if( isset($ref['id']) ) return $ref;
   else
   {
      return get_noitem();
   }
}

//------------------------------------------------------------------------------

function get_rfc_item_by_name($base, $name)
{
   $name = check_rfc_item($name);
   
   if(!($name)) return false;

   $db = mysql_init();

   $q = 'select * from ?# where `name` like "%'.$name.'%" limit 1';

   $ref = $db->selectRow($q, $base);

   if( isset($ref['id']) ) return $ref;
   else return false;
}

//------------------------------------------------------------------------------


function get_rfc_item_name($base, $id)
{
   $item = get_rfc_item($base, $id);

   if( isset($item['name']) ) return $item['name'];
   else return 'n/a';
}

function get_rfc_item_num($base, $id)
{
   $item = get_rfc_item($base, $id);

   if( isset($item['num']) ) return $item['num'];
   else return false;
}

function get_rfc_num_name($base, $num)
{
   $item = get_rfc_item_by_num($base, $num);

   if( isset($item['name']) ) return $item['name'];
   else return 'n/a';
}

//------------------------------------------------------------------------------

define('_default_level_num', get_rfc_item_num('levels', _default_level));

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function get_reference($base, $maxnum=9999)
{
   $db = mysql_init();

   $maxnum = ($maxnum>0) ? $maxnum : 1;
   
   $q = 'select * from ?# where `num` <= ?d order by `num` > 0 desc, `num`, `id`';
   
   $ref = $db->select($q, $base, $maxnum);
   
   return $ref;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function edit_reference($what, $where)
{
   if( !(is_array($what) && $what['id']>0 && strlen($what['name'])>0 && strlen($where)>0) ) return false;

   if( !(strlen($what['name'])>0 && strlen($where)>0) ) return false;

   $db = mysql_init();
   
   $q = 'update ?# set `name`=?, `num`=?d where `id`=?d';
   
   $r = $db->query($q, $where, $what['name'], $what['num'], $what['id']);
   
   return $r;
}

//------------------------------------------------------------------------------


function set_ref_order($ref, $order)
{
   if( !is_array($order) || strlen($ref)<4 ) return false;
   
   $c = count($order);
   
   $db = mysql_init();
   $q = 'update ?# set `num`=?d where `id`=?d';
   
   for($i=0; $i<$c; $i++)
   {
      $db->query($q, $ref, ($i+1), $order[$i]);
   }
   
   return $i;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function add_reference($what, $where)
{
   if( !(strlen($what)>0 && strlen($where)>0) ) return false;
   
   
   $db = mysql_init();
   
   $q = 'insert into ?# (`name`, `id`, `num`) values(?, NULL, 0)';
   
   $r = $db->query($q, $where, $what);
   
   return $r;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function del_reference($id, $where)
{
   $where = preg_replace('/[^a-z]/i', '', $where);
   
   if( !($id>0 && strlen($where)>0) ) return false;
   
   
   $db = mysql_init();

   $q = 'delete from ?# where id=?d';

   $r = $db->query($q, $where, $id);
   
   return $r;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function list_ref_opts($base, $sel, $maxnum=999)
{
   if(strlen($base) > 1)
   {
      $refs = get_reference($base, $maxnum);
      
      $c = ( is_array($refs) ) ? count($refs) : 0 ;
      
      if( $c>0 )
      {
         $opt_tpl = get_template('ref_opt_item.html');
         $opt_kwd = array('_ID_', '_NAME_', '_SELECTED_');
         
         $opts = '';
         
         for($i = 0; $i<$c; $i++)
         {
            $selected = ($sel == $refs[$i]['id']) ? 'selected' : '';
            
            $r = array($refs[$i]['id'], $refs[$i]['name'], $selected);
            
            $opts .= str_replace($opt_kwd, $r, $opt_tpl);
         }
         
         return $opts;
      }
      else return false;
   }
   else return false;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------


function html_list_refs($refs, $canedit, $justtable)
{
   $ref_tpl = get_template('ref_body.html');
   $ref_kwords = array('_ADDNEW_', '_TABLE_');

   $tbl_tpl = get_template('ref_table.html');

   $row_tpl = get_template('ref_row.html');
   $row_kwords = array('_ID_', '_ELNAME_', '_EBUTTONS_');
   
   $addnew  = ($canedit) ? get_template('ref_addnew.html') : '&nbsp;';
   $ebutton = ($canedit) ? get_template('ref_row_btt.html') : '&nbsp;';
   
   
   $c = count($refs);
   $rows = '';
   
   for($i=0; $i<$c; $i++)
   {
      $r = $refs[$i];
      
      $rb = ($canedit) ? str_replace(array('_ID_', '_NUM_'), array($r['id'], $r['num']), $ebutton) : '&nbsp;';

      $rows .= str_replace($row_kwords, array($r['id'], nbsp($r['name']), $rb), $row_tpl);
   }

   $tbl = str_replace('_ROWS_', $rows, $tbl_tpl);
   unset($rows);
   
   if($justtable) return $tbl;
   
   $ref = str_replace($ref_kwords, array($addnew, $tbl), $ref_tpl);
   unset($tbl, $addnew, $ebutton);

   return $ref;
}

//------------------------------------------------------------------------------


function get_rfc_html($type, $canedit=false, $justtable=false)
{
   $refs = get_reference($type);

   $html = html_list_refs($refs, $canedit, $justtable);

   $html .= '<script>function ref_rfctype(){return "'.$type.'";}</script>';

   return $html;
}













?>
